package com.abner.slidepicturedemo;

import com.abner.slidepicturedemo.captchalib.SwipeCaptchaView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.Toast;
import butterknife.ButterKnife;

public class MainActivity extends Activity {

    SwipeCaptchaView swipecaptchaview;
    SeekBar seekbar;
    Button changeCode;

    private int[] pictures = new int[]{R.drawable.validate, R.drawable.validate, R.drawable.validate};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        initView();
        changeCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                swipecaptchaview.createCaptcha();
                //设置拖动条能点击
                seekbar.setEnabled(true);
                //设置当前的进度为0
                seekbar.setProgress(0);
            }
        });

        //设置回调监听
        swipecaptchaview.setOnCaptchaMatchCallback(new SwipeCaptchaView.OnCaptchaMatchCallback() {
            @Override
            public void matchSuccess(SwipeCaptchaView swipeCaptchaView) {
                Toast.makeText(MainActivity.this,"恭喜你，验证成功",Toast.LENGTH_SHORT).show();
                //设置拖动
                seekbar.setEnabled(false);
            }

            @Override
            public void matchFailed(SwipeCaptchaView swipeCaptchaView) {
                Toast.makeText(MainActivity.this,"很遗憾，验证失败",Toast.LENGTH_SHORT).show();
                //设置拖动
                swipeCaptchaView.resetCaptcha();
                seekbar.setProgress(0);
            }
        });

        seekbar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                swipecaptchaview.setCurrentSwipeValue(i);

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                //设置进度条的最大值
                seekBar.setMax(swipecaptchaview.getMaxSwipeValue());
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                //进行比对截图是否一样
                swipecaptchaview.matchCaptcha();
            }
        });

        Glide.with(MainActivity.this).load(R.drawable.validate).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                //设置图片的资源
                swipecaptchaview.setImageBitmap(resource);
                //创建截图的阴影
                swipecaptchaview.createCaptcha();
            }
        });
    }
    
    private void initView(){
	    swipecaptchaview = (SwipeCaptchaView) findViewById(R.id.swipecaptchaview);
	    seekbar = (SeekBar) findViewById(R.id.seekbar);
	    changeCode = (Button) findViewById(R.id.change_code);
    }
}
